package com.ys.utils.yinsheng.yztdock.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


public class BeanUtil {

	/** 
	 * 获取类属性名 
	 *  
	 * @param pclass 
	 * @return String[]
	 */  
	public static String[] getClassDeclaredFieldNames(Class<?> pclass){
		Field[] propertyField;
		String[] returnArray;
		int count = 0;  

		if (pclass == null) {  
			return null;  
		}  
		propertyField = pclass.getDeclaredFields();  
		if (propertyField != null && propertyField.length > 0) {  
			count = propertyField.length;  
		}  

		returnArray = new String[count];
		for (int i = 0; i < count; i++) {  
			returnArray[i] = propertyField[i].getName();  
		}  
		return returnArray;  
	}

	public static Map<String,Object> getBean2Map(Object obj) {
		Class<?> clazz = obj.getClass();
		Field[] fieldArray = clazz.getDeclaredFields();
		Field.setAccessible(fieldArray, true);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			for (Field field : fieldArray) {
				if(field.get(obj) == null) {
					continue;
				}
				map.put(field.getName(), field.get(obj));
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}



//	public static void main(String[] args) throws Exception {
//
//		String[] s = BeanUtil.getClassDeclaredFieldNames(ReqMessage.class);
//		Arrays.sort(s);
//		for (String string : s) {
//			System.out.println(string);
//		}
//
//	}


}
